package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.LinkedHashMap;
import java.util.Map;

public class CancelAppointmentTransfer extends AbstractTransfer {

    public CancelAppointmentTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String appointId = (String) params.get("appointId");
        String user = (String) params.get("user");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("APPTED_ID", appointId).addAttribute("CREDENTIALS_NO", idCard).addAttribute("CREDENTIALS_TYPE", "1");
        dataRowElement.addAttribute("PHONE_NUMBER", user);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new LinkedHashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        resMap.put("code", executeStatus);
        resMap.put("message", executeMessage);
        return resMap;
    }
}
